package com.dime.physical.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.dime.physical.domain.dto.PhysicalDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.dime.physical.domain.DimePhysicalregister;
import com.dime.physical.service.IDimePhysicalregisterService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 人员登记Controller
 * 
 * @author Tiny
 * @date 2023-09-14
 */
@RestController
@RequestMapping("/physicalregister")
public class DimePhysicalregisterController extends BaseController {
    @Autowired
    private IDimePhysicalregisterService dimePhysicalregisterService;

    /**
     * 查询人员登记列表
     */
    @RequiresPermissions("physical:physicalregister:list")
    @GetMapping("/list")
    public TableDataInfo list(DimePhysicalregister dimePhysicalregister) {
        startPage();
        List<DimePhysicalregister> list = dimePhysicalregisterService.selectDimePhysicalregisterList(dimePhysicalregister);
        return getDataTable(list);
    }

    /**
     * 导出人员登记列表
     */
    @RequiresPermissions("physical:physicalregister:export")
    @Log(title = "人员登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DimePhysicalregister dimePhysicalregister) {
        List<DimePhysicalregister> list = dimePhysicalregisterService.selectDimePhysicalregisterList(dimePhysicalregister);
        ExcelUtil<DimePhysicalregister> util = new ExcelUtil<DimePhysicalregister>(DimePhysicalregister.class);
        util.exportExcel(response, list, "人员登记数据");
    }

    /**
     * 获取人员登记详细信息
     */
    @RequiresPermissions("physical:physicalregister:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(dimePhysicalregisterService.selectDimePhysicalById(id));
    }

    /**
     * 新增人员登记
     */
    @RequiresPermissions("physical:physicalregister:add")
    @Log(title = "人员登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DimePhysicalregister dimePhysicalregister) {
        return toAjax(dimePhysicalregisterService.insertDimePhysicalregister(dimePhysicalregister));
    }

    /**
     * 修改人员登记
     */
    @RequiresPermissions("physical:physicalregister:edit")
    @Log(title = "人员登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DimePhysicalregister dimePhysicalregister) {
        return toAjax(dimePhysicalregisterService.updateDimePhysicalregister(dimePhysicalregister));
    }


    @RequiresPermissions("physical:physicalregister:editPhysical")
    @Log(title = "修改人员登记信息", businessType = BusinessType.OTHER)

    @PutMapping(value = "/editPhysical")
    public AjaxResult editPhysical(@RequestBody PhysicalDto physicalDto) {
        return toAjax(dimePhysicalregisterService.updateDimePhysical(physicalDto));
    }


    /**
     * 删除人员登记
     */
    @RequiresPermissions("physical:physicalregister:remove")
    @Log(title = "人员登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(dimePhysicalregisterService.deleteDimePhysicalregisterByIds(ids));
    }


    @RequiresPermissions("physical:physicalregister:getRegisterInfo")
    @Log(title = "根据身份证获取登记信息", businessType = BusinessType.OTHER)
    @GetMapping(value = "getRegisterInfo/{idno}")
    public AjaxResult getRegisterInfo(@PathVariable("idno") String idno) {
        return success(dimePhysicalregisterService.getBQregisterInfoByidnoOrvid(idno, ""));
    }


    @RequiresPermissions("physical:physicalregister:getRegisterInfobyVid")
    @Log(title = "根据体检号获取登记信息", businessType = BusinessType.OTHER)
    @GetMapping(value = "getRegisterInfobyVid/{vid}")
    public AjaxResult getRegisterInfobyVid(@PathVariable("vid") String vid) {
        return success(dimePhysicalregisterService.getBQregisterInfoByidnoOrvid("", vid));
    }


    @RequiresPermissions("physical:physicalregister:SavePhysical")
    @Log(title = "扁鹊登记保存", businessType = BusinessType.OTHER)
    @PostMapping("/SavePhysical")
    public AjaxResult SavePhysical(@RequestBody PhysicalDto physicalDto) {
        return toAjax(dimePhysicalregisterService.insertBQregister(physicalDto));
    }


    //根据Vid判断登记信息是否存在
    @RequiresPermissions("physical:physicalregister:checkregisterInfoByVid")
    @Log(title = "根据Vid判断登记信息是否存在", businessType = BusinessType.OTHER)
    @GetMapping("/checkregisterInfoByVid/{vid}")
    public AjaxResult checkregisterInfoByVid(@PathVariable("vid") String vid) {
        return success(dimePhysicalregisterService.checkregisterInfoByVid(vid));
    }


    //删除原来信息重新登记
    @RequiresPermissions("physical:physicalregister:ReRegisterInfo")
    @Log(title = "删除原来信息重新登记", businessType = BusinessType.OTHER)
    @PostMapping("/ReRegisterInfo")
    public AjaxResult ReRegisterInfo(@RequestBody PhysicalDto physicalDto) {
        DimePhysicalregister dimePhysicalregister1 = dimePhysicalregisterService.selectDimePhysicalregisterById(physicalDto.getId());

        if (dimePhysicalregister1.getCompletStatus() != null && Integer.parseInt(dimePhysicalregister1.getCompletStatus()) > 1) {
            return error("人员已进入主检！");
        }

        dimePhysicalregisterService.deleteDimePhysicalregisterById(dimePhysicalregister1);
        return success(dimePhysicalregisterService.insertBQregister(physicalDto));
    }


    @RequiresPermissions("physical:physicalregister:getPhysicalInfo")
    @Log(title = "获取登记信息", businessType = BusinessType.OTHER)
    @PostMapping(value = "/getPhysicalInfo")
    public AjaxResult getPhysicalInfo(@RequestBody DimePhysicalregister dimePhysicalregister) {
        return success(dimePhysicalregisterService.getPhysicalInfo(dimePhysicalregister));
    }


    //    @RequiresPermissions("physical:physicalregister:getReport")
    @Log(title = "获取报告", businessType = BusinessType.OTHER)
    @PostMapping(value = "/getReport")
    public AjaxResult getReport(@RequestBody List<DimePhysicalregister> dimePhysicalregisters) {
        int rows = 0;
        for (DimePhysicalregister dimePhysicalregister : dimePhysicalregisters) {

            if (dimePhysicalregister.getCompletStatus() != null && Integer.parseInt(dimePhysicalregister.getCompletStatus()) > 1) {
                continue;
            }
            rows += dimePhysicalregisterService.getReport(dimePhysicalregister);
        }
        return success(rows);
    }

}
